
#include "config.h"
#include "utils.h"

#define TAG "cfg"

void Config::set_wifi_ssid(String value)
{
    if(wifi_ssid == value) return;
    ESP_LOGI(TAG,"wifi_ssid:%s->%s\n",wifi_ssid.c_str(),value.c_str());
    wifi_ssid = value;
    prefs.putString(KEY_WIFI_SSID, value);
    return;
}
String Config::get_wifi_ssid()
{
    return wifi_ssid;
}

void Config::set_wifi_pwd(String value)
{
    if(wifi_pwd == value) return;
    ESP_LOGI(TAG,"wifi_pwd:%s->%s\n",wifi_pwd.c_str(),value.c_str());
    wifi_pwd = value;
    prefs.putString(KEY_WIFI_PWD, value);
    return;
}
String Config::get_wifi_pwd()
{
    return wifi_pwd;
}


void Config::set_wifi_mode(uint16_t value)
{
    if(wifi_mode == value) return;
    limit(value, (uint16_t)0, (uint16_t)2);
    ESP_LOGI(TAG,"wifi_mode:%d -> %d\n", wifi_mode, value);
    wifi_mode = value;
    prefs.putUInt(KEY_WIFI_MODE, wifi_mode);
    return;
}
uint16_t Config::get_wifi_mode()
{
    return wifi_mode;
}

void Config::set_wifi_ip(IPAddress p)
{
    if(wifi_ip == p) return;
    ESP_LOGI(TAG,"wifi_ip:%s -> %s\n", wifi_ip.toString().c_str(), p.toString().c_str());
    wifi_ip = p;
    prefs.putString(KEY_WIFI_IP, p.toString());
    return;
}
IPAddress Config::get_wifi_ip()
{
    return wifi_ip;
}

void Config::set_wifi_subnet(IPAddress p)
{
    if(wifi_subnet == p) return;
    ESP_LOGI(TAG,"wifi_subnet:%s -> %s\n", wifi_subnet.toString().c_str(), p.toString().c_str());
    wifi_subnet = p;
    prefs.putString(KEY_WIFI_SUBNET, p.toString());
    return;
}
IPAddress Config::get_wifi_subnet()
{
    return wifi_subnet;
}
void Config::set_wifi_gw(IPAddress p)
{
    if(wifi_gw == p) return;
    ESP_LOGI(TAG,"wifi_gw:%s -> %s\n", wifi_gw.toString().c_str(), p.toString().c_str());
    wifi_gw = p;
    prefs.putString(KEY_WIFI_GW, p.toString());
    return;
}
IPAddress Config::get_wifi_gw()
{
    return wifi_gw;
}


void Config::set_dns(uint8_t index, IPAddress p)
{
    char key[10] = {0};
    sprintf(key, "dns%d", index);
    if(dns[index] == p) return;
    ESP_LOGI(TAG,"dns[%d]:%s -> %s\n",index, dns[index].toString().c_str(), p.toString().c_str());
    dns[index] = p;
    prefs.putString(key, p.toString());
    return;
}
IPAddress Config::get_dns(uint8_t index)
{
    return dns[index];
}

void Config::set_ntp_server(String value)
{
    if(ntp_server == value) return;
    ESP_LOGI(TAG,"ntp_server:%s -> %s\n", ntp_server.c_str(), value.c_str());
    ntp_server = value;
    prefs.putString(KEY_NTP_SERVER, value);
    return;
}
String Config::get_ntp_server()
{
    return ntp_server;
}
